#include "settingcurvedialog.h"
#include "ui_settingcurvedialog.h"
#include "legendmodel.h"
#include "abstractplot2d.h"
#include "colorbutton.h"
#include "legenddelegate.h"
#include <QMessageBox>


SettingCurveDialog::SettingCurveDialog(AbstractPlot2D *plotter, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingCurveDialog),
    _plotter(plotter)
{
    ui->setupUi(this);

    QStringList legend;
    foreach (CurveData cd, _plotter->curvesList()) {
        legend.append(cd.legend());
        CurveSettings cs;
        cs.color = cd.color();
        cs.penStyle = cd.penStyle();
        cs.penWidth = cd.penWidth();
        cs.symbol = (int)cd.symbol();
        cs.symbolSize = cd.symbolSize();
        _curveSettingsList.append(cs);
    }
    for (int i = 0; i < _plotter->showCurvesList().size(); ++i) {
        _curveSettingsList[i].showCurve = _plotter->showCurvesList().at(i);
    }

    model = new LegendModel(legend, this);

    ui->treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->treeView->setModel(model);
    ui->treeView->setItemDelegateForColumn(0, new LegendDelegate(this));
    ui->treeView->setCurrentIndex(model->index(0,0));

    ui->lineStyleComboBox->addItem("SolidLine", QVariant(Qt::SolidLine));
    ui->lineStyleComboBox->addItem("DashLine", QVariant(Qt::DashLine));
    ui->lineStyleComboBox->addItem("DotLine", QVariant(Qt::DotLine));
    ui->lineStyleComboBox->addItem("DashDotLine", QVariant(Qt::DashDotLine));
    ui->lineStyleComboBox->addItem("DashDotDotLine", QVariant(Qt::DashDotDotLine));
    ui->lineStyleComboBox->addItem("NoLine", QVariant(Qt::NoPen));

    ui->symbolComboBox->addItem("NoSymbol", QVariant(NoSymbol));
    ui->symbolComboBox->addItem("Cross", QVariant(Cross));
    ui->symbolComboBox->addItem("Circle", QVariant(Circle));
    ui->symbolComboBox->addItem("Square", QVariant(Square));
    ui->symbolComboBox->addItem("TriangleUp", QVariant(TriangleUp));
    ui->symbolComboBox->addItem("TriangleDown", QVariant(TriangleDown));
    ui->symbolComboBox->addItem("Rhombus", QVariant(Rhombus));

    _clrBtn = new ColorButton;
    ui->horizontalLayout_5->addWidget(_clrBtn);

    connect(ui->treeView, SIGNAL(activated(QModelIndex)), this, SLOT(changeCurve(QModelIndex)));
    connect(ui->lineStyleComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeLineStyle(int)));
    connect(ui->doubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeLineWidth(double)));
    connect(ui->symbolComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeSymbol(int)));
    connect(ui->symbolSizeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeSymbolSize(int)));
    connect(_clrBtn, SIGNAL(colorChanged(QColor)), this, SLOT(changeColor(QColor)));
    connect(ui->showCurveCheckBox, SIGNAL(clicked(bool)), this, SLOT(changeShowCurve(bool)));
    connect(ui->applyBtn, SIGNAL(clicked()), this, SLOT(apply()));
    connect(ui->removeBtn, SIGNAL(clicked()), this, SLOT(removeCurve()));
    initCurve();


}
//-----------------------------------------------------------------------------

SettingCurveDialog::~SettingCurveDialog()
{
    delete ui;
}
//-----------------------------------------------------------------------------

void SettingCurveDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
//-----------------------------------------------------------------------------

void SettingCurveDialog::apply()
{
    for (int i = 0; i < _curveSettingsList.size(); ++i) {
        CurveSettings cs = _curveSettingsList.at(i);
        _plotter->curvesList()[i].setColor(cs.color);
        _plotter->curvesList()[i].setPenStyle(cs.penStyle);
        _plotter->curvesList()[i].setPenWidth(cs.penWidth);
        _plotter->curvesList()[i].setSymbol((Symbol)cs.symbol);
        _plotter->curvesList()[i].setSymbolSize(cs.symbolSize);
        _plotter->showCurvesList()[i] = cs.showCurve;
    }
    _plotter->updatePlot();
}
//-----------------------------------------------------------------------------

bool SettingCurveDialog::removeCurve()
{
    int rowCount = model->rowCount();
    if (rowCount != 0) {

        int ret = QMessageBox::warning(this, tr("LightPlot2D"),
                                        tr("Are you sure you want to delete the curve?"),
                                        QMessageBox::Ok | QMessageBox::Cancel);

        if (ret == QMessageBox::Ok) {
            int row = ui->treeView->currentIndex().row();
            model->removeCurve(row);
            _curveSettingsList.remove(row);
            _plotter->removeCurve(row);
            if (row == rowCount-1) {
                ui->treeView->setCurrentIndex(model->index(--row, 0));
            } else
                ui->treeView->setCurrentIndex(model->index(row, 0));
            return true;
        }
    }
    return false;
}
//-----------------------------------------------------------------------------

void SettingCurveDialog::changeCurve(const QModelIndex &index)
{
    int data = (int)_curveSettingsList.at(index.row()).penStyle;
    int i = ui->lineStyleComboBox->findData(QVariant(data));
    ui->lineStyleComboBox->setCurrentIndex(i);

    ui->doubleSpinBox->setValue(_curveSettingsList.at(index.row()).penWidth);
    data = (int)_curveSettingsList.at(index.row()).symbol;
    i = ui->symbolComboBox->findData(QVariant(data));
    ui->symbolComboBox->setCurrentIndex(i);

    ui->symbolSizeSpinBox->setValue(_curveSettingsList.at(index.row()).symbolSize);

    _clrBtn->setColor(_curveSettingsList.at(index.row()).color);

    ui->showCurveCheckBox->setChecked(_curveSettingsList.at(index.row()).showCurve);
}
//-----------------------------------------------------------------------------

void SettingCurveDialog::initCurve()
{
    if (_curveSettingsList.isEmpty())
        return;

    int data = (int)_curveSettingsList.at(0).penStyle;
    int i = ui->lineStyleComboBox->findData(QVariant(data));
    ui->lineStyleComboBox->setCurrentIndex(i);

    ui->doubleSpinBox->setValue(_curveSettingsList.at(0).penWidth);
    data = (int)_curveSettingsList.at(0).symbol;
    i = ui->symbolComboBox->findData(QVariant(data));
    ui->symbolComboBox->setCurrentIndex(i);

    ui->symbolSizeSpinBox->setValue(_curveSettingsList.at(0).symbolSize);

    _clrBtn->setColor(_curveSettingsList.at(0).color);

    ui->showCurveCheckBox->setChecked(_curveSettingsList.at(0).showCurve);
}
//-----------------------------------------------------------------------------

void SettingCurveDialog::changeLineStyle(int index)
{
    int row = ui->treeView->currentIndex().row();
    _curveSettingsList[row].penStyle = (Qt::PenStyle)ui->lineStyleComboBox->itemData(index).toInt();
}
//-----------------------------------------------------------------------------

void SettingCurveDialog::changeLineWidth(double width)
{
    int row = ui->treeView->currentIndex().row();
    _curveSettingsList[row].penWidth = width;
}
//-----------------------------------------------------------------------------

void SettingCurveDialog::changeSymbol(int symbol)
{
    int row = ui->treeView->currentIndex().row();
    _curveSettingsList[row].symbol = symbol;
}
//-----------------------------------------------------------------------------

void SettingCurveDialog::changeSymbolSize(int size)
{
    int row = ui->treeView->currentIndex().row();
    _curveSettingsList[row].symbolSize = size;
}
//-----------------------------------------------------------------------------

void SettingCurveDialog::changeColor(const QColor &color)
{
    int row = ui->treeView->currentIndex().row();
    _curveSettingsList[row].color = color;
}
//-----------------------------------------------------------------------------

void SettingCurveDialog::changeShowCurve(bool show)
{
    int row = ui->treeView->currentIndex().row();
    _curveSettingsList[row].showCurve = show;
}
//-----------------------------------------------------------------------------
